package com.qf.taobei.controller;


import com.qf.taobei.entity.Address;
import com.qf.taobei.entity.RespBean;
import com.qf.taobei.entity.User;
import com.qf.taobei.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "收货地址模块")
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 添加收货地址
     * @param session
     * @param address
     * @return
     */
    @ApiOperation("添加收货地址")
    @PostMapping("/addAddr")
    public RespBean addAddress(HttpSession session,Address address){
//        User user = (User) session.getAttribute("user");
//        address.setUid(user.getId());
        address.setUid(1);
        Integer res = addressService.addAddress(address);
        if (res == 0){
            return RespBean.respError("系统繁忙");
        }
        return RespBean.respSuccess("添加成功");
    }


    /**
     * 修改收货地址
     * @param address
     * @return
     */
    @ApiOperation("修改收货地址")
    @PostMapping("/updateAddr")
    public RespBean updateAddress(Address address){
        Integer res = addressService.updateAddress(address);
        if (res == 0){
            return RespBean.respError("系统繁忙");
        }
        return RespBean.respSuccess("修改成功");
    }


    /**
     * 用户获取所有收货地址
     * @param session
     * @return
     */
    @ApiOperation("用户查看所有收货地址")
    @GetMapping("/getAllAddr")
    public RespBean getAllAddress(HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Address> addressList = addressService.queryAllAddress(user.getId());
//        List<Address> addressList = addressService.queryAllAddress(1);
        return RespBean.respSuccess("查询成功",addressList);
    }


    /**
     * 修改默认收货地址
     * @param id
     * @return
     */
    @ApiOperation("用户修改默认收货地址")
    @PostMapping("/updateAddrDef")
    public RespBean updateAddrDef(HttpSession session,Integer id){
//        User user = (User) session.getAttribute("user");
//        Integer res = addressService.updateAddrDef(user.getId(),id);
        Integer res = addressService.updateAddrDef(1,id);
        if (res == 0){
            return RespBean.respError("系统繁忙");
        }
        return RespBean.respSuccess("修改成功");
    }


    /**
     * 删除收货地址
     * @param id 收货地址id
     * @return
     */
    @ApiOperation("删除收货地址")
    @PostMapping("/delAddr")
    public RespBean delAddr(Integer id){
        Integer res = addressService.delAddr(id);
        if (res == 0){
            return RespBean.respError("系统繁忙");
        }
        return RespBean.respSuccess("删除成功");
    }

}
